package top.hmtools.pojo;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import top.hmtools.base.StringTools;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 用户请求日志信息实体类
 * @author HyboJ
 *
 */
public class RequestLogBean implements Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUID = -2487991715007020123L;

	/**
	 * 当前请求的身份识别ID
	 */
	private String requestID;

	/**
	 * 当前请求的来源（上一跳）
	 */
	private String refferURL;

	/**
	 * 当前请求的URL地址
	 */
	private String requestURL;
	
	/**
	 * 当前请求的资源URI地址，20170406.0905 hybo 新增
	 */
	private String requestURI;
	
	/**
	 * 当前请求的查询字符串（地址栏get请求时，URL中“？”之后的字符串）20170406.0905 hybo 新增
	 */
	private String queryString;

	/**
	 * 当前请求中的 header 头信息
	 */
	private Map<String, String> requestHeaderMap;

	/**
	 * 当前请求中的 header 头信息，json字符串格式
	 */
	private String requestHeaderJsonString;

	/**
	 * 当前请求中携带的参数信息
	 */
	private Map<String, String> requestParamsMap;

	/**
	 * 当前请求中携带的参数信息，json字符串格式
	 */
	private String requestParamsJsonString;

	/**
	 * 当前请求时的时间戳
	 */
	private Date requestDateTime;

	/**
	 * 对当前请求的业务处理完后的返回响应时间戳
	 */
	private Date responseDateTime;

	/**
	 * 对当前请求的业务处理的response状态（HTTP响应状态码）
	 */
	private String responseCode;

	/**
	 * 页面停留时间，单位：毫秒
	 */
	private long residenceTime;
	
	/**
	 * 终端类型：-1 未知
	 */
	public static final Integer TERMINALTYPE_UNKNOW = -1;
	
	/**
	 * 终端类型：0 PC浏览器
	 */
	public static final Integer TERMINALTYPE_PC = 0;
	
	/**
	 * 终端类型：1.h5
	 */
	public static final Integer TERMINALTYPE_H5 = 1;
	
	/**
	 * 终端类型：2 android 客户端APP
	 */
	public static final Integer TERMINALTYPE_ANDROID = 2;
	
	/**
	 * 终端类型： 3 ios
	 */
	public static final Integer TERMINALTYPE_IOS = 3;
	
	/**
	 * 终端类型：-1.未知 ； 0.PC浏览器 ； 1.h5 ； 2.android ； 3 ios ； 20170406.1056 hybo 新增
	 */
	private Integer terminalType;
	
	/**
	 * 当前访问用户的sessionID号 20170406.1056 hybo 新增
	 */
	private String sessionID;
	
	/**
	 * 客户操作系统名称  20170407.0932  hybo 新增
	 */
	private String osName;
	
	/**
	 * 访问者ID，此值由js生成，并存储在用户浏览器的cookie或者storage中，用于区分是否同一台终端同一浏览器在不同时间访问
	 * <br> 20170417.1740  hybo 新增
	 */
	private String visitorID;

	/**
	 * 当前请求的资源URI地址，20170406.0905 hybo 新增
	 * @return
	 */
	public String getRequestURI() {
		return requestURI;
	}

	/**
	 * 当前请求的资源URI地址，20170406.0905 hybo 新增
	 * @param requestURI
	 */
	public void setRequestURI(String requestURI) {
		this.requestURI = requestURI;
	}

	public String getRequestID() {
		return requestID;
	}

	public void setRequestID(String requestID) {
		this.requestID = requestID;
	}

	public String getRefferURL() {
		return refferURL;
	}

	public void setRefferURL(String refferURL) {
		this.refferURL = refferURL;
	}

	public String getRequestURL() {
		return requestURL;
	}

	public void setRequestURL(String requestURL) {
		this.requestURL = requestURL;
	}

	public Map<String, String> getRequestHeaderMap() {
		Map<String, String> result = new HashMap<String, String>();

		if (this.requestHeaderMap == null
				&& StringTools.isNotBlank(this.requestHeaderJsonString)) {
			JSONObject jsonObject = JSON
					.parseObject(this.requestHeaderJsonString);
			Set<Entry<String, Object>> entrySet = jsonObject.entrySet();
			Iterator<Entry<String, Object>> iterator = entrySet.iterator();
			while (iterator.hasNext()) {
				Entry<String, Object> next = iterator.next();
				result.put(next.getKey(), JSON.toJSONString( next.getValue()));
			}
		} else {
			result = this.requestHeaderMap;
		}
		return result;
	}

	public void setRequestHeaderMap(Map<String, String> requestHeaderMap) {
		this.requestHeaderMap = requestHeaderMap;
	}

	public Map<String, String> getRequestParamsMap() {
		Map<String, String> result = new HashMap<String, String>();

		if (this.requestParamsMap == null 	&& StringTools.isNotBlank(this.requestParamsJsonString)) {
			JSONObject jsonObject = JSON.parseObject(this.requestParamsJsonString);
			Set<Entry<String, Object>> entrySet = jsonObject.entrySet();
			Iterator<Entry<String, Object>> iterator = entrySet.iterator();
			while (iterator.hasNext()) {
				Entry<String, Object> next = iterator.next();
				result.put(next.getKey(), JSON.toJSONString( next.getValue()));
			}
		} else {
			result = this.requestParamsMap;
		}
		return result;
	}

	public void setRequestParamsMap(Map<String, String> requestParamsMap) {
		this.requestParamsMap = requestParamsMap;
	}

	public Date getRequestDateTime() {
		return requestDateTime;
	}

	public void setRequestDateTime(Date requestDateTime) {
		this.requestDateTime = requestDateTime;
	}

	public Date getResponseDateTime() {
		return responseDateTime;
	}

	public void setResponseDateTime(Date responseDateTime) {
		this.responseDateTime = responseDateTime;
	}

	public String getResponseCode() {
		return responseCode;
	}

	public void setResponseCode(String responseCode) {
		this.responseCode = responseCode;
	}

	/**
	 * 获取用户页面停留时间，单位：毫秒
	 * @return
	 */
	public long getResidenceTime() {
		return residenceTime;
	}

	/**
	 * 设置用户页面停留时间，单位：毫秒
	 * @param residenceTime
	 */
	public void setResidenceTime(long residenceTime) {
		this.residenceTime = residenceTime;
	}

	public String getRequestHeaderJsonString() {
		String result = null;
		try {
			if(StringTools.isBlank(this.requestHeaderJsonString)&&this.requestHeaderMap!=null){
				result = JSON.toJSONString(this.requestHeaderMap);
			}else{
				result = this.requestHeaderJsonString;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return requestHeaderJsonString;
		}
		return result;
	}

	public void setRequestHeaderJsonString(String requestHeaderJsonString) {
		this.requestHeaderJsonString = requestHeaderJsonString;
	}

	public String getRequestParamsJsonString() {
		String result = null;
		try {
			if(StringTools.isBlank(this.requestParamsJsonString)&&this.requestParamsMap!=null){
				result = JSON.toJSONString(this.requestParamsMap);
			}else{
				result = this.requestParamsJsonString;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return requestParamsJsonString;
		}
		return result;
	}

	public void setRequestParamsJsonString(String requestParamsJsonString) {
		this.requestParamsJsonString = requestParamsJsonString;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public Integer getTerminalType() {
		return terminalType;
	}

	public void setTerminalType(Integer terminalType) {
		this.terminalType = terminalType;
	}

	public String getSessionID() {
		return sessionID;
	}

	public void setSessionID(String sessionID) {
		this.sessionID = sessionID;
	}

	public String getOsName() {
		return osName;
	}

	public void setOsName(String osName) {
		this.osName = osName;
	}

	public String getVisitorID() {
		return visitorID;
	}

	public void setVisitorID(String visitorID) {
		this.visitorID = visitorID;
	}

	@Override
	public String toString() {
		return "RequestLogBean [requestID=" + requestID + ", refferURL="
				+ refferURL + ", requestURL=" + requestURL + ", requestURI="
				+ requestURI + ", queryString=" + queryString
				+ ", requestHeaderMap=" + requestHeaderMap
				+ ", requestHeaderJsonString=" + requestHeaderJsonString
				+ ", requestParamsMap=" + requestParamsMap
				+ ", requestParamsJsonString=" + requestParamsJsonString
				+ ", requestDateTime=" + requestDateTime
				+ ", responseDateTime=" + responseDateTime + ", responseCode="
				+ responseCode + ", residenceTime=" + residenceTime
				+ ", terminalType=" + terminalType + ", sessionID=" + sessionID
				+ ", osName=" + osName + ", visitorID=" + visitorID + "]";
	}

	
	

}
